package cn.iocoder.yudao.module.product.service.specifications;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.specifications.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.specifications.SpecificationsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.product.dal.mysql.specifications.SpecificationsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 商品规格 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SpecificationsServiceImpl implements SpecificationsService {

    @Resource
    private SpecificationsMapper specificationsMapper;

    @Override
    public Integer createSpecifications(SpecificationsSaveReqVO createReqVO) {
        // 插入
        SpecificationsDO specifications = BeanUtils.toBean(createReqVO, SpecificationsDO.class);
        specificationsMapper.insert(specifications);
        // 返回
        return specifications.getId();
    }

    @Override
    public void updateSpecifications(SpecificationsSaveReqVO updateReqVO) {
        // 校验存在
        validateSpecificationsExists(updateReqVO.getId());
        // 更新
        SpecificationsDO updateObj = BeanUtils.toBean(updateReqVO, SpecificationsDO.class);
        specificationsMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecifications(Integer id) {
        // 校验存在
        validateSpecificationsExists(id);
        // 删除
        specificationsMapper.deleteById(id);
    }

    private void validateSpecificationsExists(Integer id) {
        if (specificationsMapper.selectById(id) == null) {
            throw exception(SPECIFICATIONS_NOT_EXISTS);
        }
    }

    @Override
    public SpecificationsDO getSpecifications(Integer id) {
        return specificationsMapper.selectById(id);
    }

    @Override
    public PageResult<SpecificationsDO> getSpecificationsPage(SpecificationsPageReqVO pageReqVO) {
        return specificationsMapper.selectPage(pageReqVO);
    }

}